use std::fmt::Display;
use anyhow::bail;

#[derive(Clone, Copy , Default , Eq , PartialEq , Ord , PartialOrd)]
pub enum Scheme {
    #[default] // 编译器不知道UrlScheme的默认值是哪一个 所以在Regular上面标注一下
    Regular ,
    Search,
    SearchItem ,
    Archive ,
}

impl TryFrom<&[u8]> for Scheme {
    type Error = anyhow::Error ; 
    fn try_from(s: &[u8]) -> Result<Self , Self::Error> {
        Ok(match s {
            b"regular" => Scheme::Regular, 
            b"search" => Scheme::Search, 
            b"archive" => Scheme::Archive , 
            _ => bail!("Unknown URL scheme:{}" , String::from_utf8_lossy(s)) ,
        })
    }
}

impl Display for Scheme {
    fn fmt(&self, f :&mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Scheme::Regular => write!(f , "regular") , 
            Scheme::Search => write!(f , "search") , 
            Scheme:: SearchItem => write!(f , "search-item") , 
            Scheme::Archive => write!(f , "archive") , 
        }
    }
}